package com.kinghood.productcenter.common.model.dto.product;

import com.baomidou.mybatisplus.annotation.TableField;
import com.kinghood.productcenter.common.model.enums.base.AttributeValueCodeEnum;
import com.kinghood.productcenter.service.product.handler.ProductBaseHandler;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-01-11 17:08
 **/
@Data
public class AttributeByProductIdDTO implements Serializable {

    /**
     * 业务id（商品id，skuId，sku附件id，sku供应商id）
     */
    private Long businessId;
    /**
     * 业务类型（0：商品，1：sku，2：sku附件，3：sku供应商）
     */
    private Integer businessType;
    /**
     * 触发的属性id
     */
    private Long attributeId;
    /**
     * 属性编码
     */
    private String attributeCode;
    /**
     * 属性名
     */
    private String attributeName;
    /**
     * 属性编码（表示数字，日期）
     */
    private String valueCode;
    /**
     * 属性，逗号隔开，用于搜索
     */
    private String valueFind;
    /**
     * 值
     * */
    private List<ValueDTO> valueList;

    public String getValueName(){

        String valueFind = this.getValueFind();
        String valueCode = this.getValueCode();
        boolean r = ProductBaseHandler.returnValue(valueFind,valueCode);
        if(r){
            return valueFind;
        }
        if (Objects.equals(valueCode, AttributeValueCodeEnum.DATE.getCode())) {
            return valueFind;
        }
        // 单个返回true
        if (Objects.equals(valueCode, AttributeValueCodeEnum.CHECK_BOX.getCode()) && Objects.equals("true",valueFind)) {
            return valueList.get(0).getValue();
        }
        String[] split = valueFind.split(",");
        if(!CollectionUtils.isEmpty(this.getValueList()) && split.length > 0){
            StringBuilder sb = new StringBuilder();
            Map<String, String> valueMap = this.getValueList().stream().collect(Collectors.toMap(k -> k.getId().toString(), v -> v.getValue(), (k1, k2) -> k1));
            for (String s : split) {
                String v = valueMap.get(s);
                sb.append(v).append(" ");
            }
            return sb.toString();
        }

        return null;
    }
}
